package domain;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ProjectVector extends Vector{
	private Project project;
	private Criteria criteria;
	
	public Project getProject() {
		return project;
	}
	public Criteria getCriteria() {
		return criteria;
	}
	public void setProject(Project project) {
		this.project = project;
	}
	public void setCriteria(Criteria criteria) {
		this.criteria = criteria;
	}
	
	public void insert() throws SQLException{
		Connection conn = getConnection();
		PreparedStatement ps = null;
		 StringBuffer sql = new StringBuffer();
			 sql.append("insert into vector(priority, type, sub_criteria_id, project_id, alternative_id, criteria_id) ");
			 sql.append(" values(?, ?, ?, ?, ?, ?)");
		 int index =1;
		 ps = conn.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS);
		 	ps.setDouble(index++, this.priority);
		 	ps.setString(index++, this.type);
		 	ps.setLong(index++, this.criteria.getId());
		 	ps.setLong(index++, this.project.getId());
		 	ps.setLong(index++, 0);
		 	ps.setLong(index++, 0);
		 ps.executeUpdate();
		 
		 ResultSet resultSet = ps.getGeneratedKeys();
			if (resultSet.next()) {
				long generatedid = resultSet.getLong(1);
				ps.close();
				conn.close();
				this.id = generatedid;
			} else {
				 System.out.println("Cannot Get Genereated ID!!!!");
			}
	}
}
